# obot_chat.tcl
#   This script provides you with a more civilized way to
#   talk to your eggdrop.
#
# Copyright (C) 2005, Remco B. Brink <remco@rc6.org>
#
# CHANGELOG
#  1.6 - Disabled showing the local time, since functionality
#        is provided by weather.tcl
#  1.5 - Slap is now a voiced command and other small fixes.
#  1.4 - Exterminate now responds to "me" as well.
#  1.3 - Now using the standard eggdrop access levels again
#        and added a lot of new commands
#  1.2 - Replaced $botnick with hardcoded botname for the
#        time being.
#  1.1 - Initial release
#

set script_version "1.6"

# Master only commands
bind pubm - "*dalek: rehash*" polite:rehash
bind pubm - "*dalek: save*" polite:save
bind pubm - "*dalek: backup*" polite:backup
bind pubm - "*dalek: die*" polite:die
bind pubm - "*dalek: restart*" polite:restart
bind pubm - "*dalek: reload*" polite:reload
bind pubm - "*dalek: addchan*" polite:addchan
bind pubm - "*dalek: delchan*" polite:delchan
bind pubm - "*dalek: chanset*" polite:chanset
bind pubm - "*dalek: adduser*" polite:adduser
bind pubm - "*dalek: deluser*" polite:deluser
bind pubm - "*dalek: chattr*" polite:chattr

# Channel Operator binds
#bind pubm o|o "*dalek: op*" polite:op
bind pubm - "*dalek: deop*" polite:deop
bind pubm - "*dalek: validuser*" polite:validuser

# Channel Voice binds
bind pubm - "*dalek: exterminate*" polite:dalek
bind pubm - "*dalek: thank*" polite:thanks
bind pubm v|v "*dalek: kick*" polite:kick
bind pubm v|v "*dalek: voice*" polite:voice
bind pubm v|v "*dalek: devoice*" polite:devoice
bind pubm v|v "*dalek: unvoice*" polite:devoice
bind pubm v|v "*dalek: ban*" polite:ban
bind pubm v|v "*dalek: xban*" polite:xban
bind pubm - "*dalek: unban*" polite:unban
bind pubm v|v "*dalek: xunban*" polite:xunban
bind pubm - "*dalek: dance*" polite:music
bind pubm v|v "*dalek: invite*" polite:invite
bind pubm v|v "*dalek: resetbans*" polite:resetbans
bind pubm v|v "*dalek: say*" polite:say
bind pubm - "*dalek: ping*" polite:ping
bind pubm - "*dalek: slap*" polite:slap
bind pubm - "*dalek: kiss*" polite:kiss

# Commands for the rest of us
bind pubm - "*dalek: time*" polite:time
bind pubm - "*dalek: server*" polite:server
bind pubm - "*dalek: date*" polite:date
bind pubm - "*dalek: status*" polite:status
bind pubm - "*dalek: uptime*" polite:uptime
bind pubm - "*dalek: whoami*" polite:whoami
bind pub - !pm polite:pm

#Slap sbody on #channel
proc polite:slap {nick host handle channel testes} {
  if {([isop $nick $channel]) || ([ishalfop $nick $channel]) || ([isvoice $nick $channel])} {
    global botnick slaps slapoutput randnum
    set who [lindex $testes 2]
    if {[string tolower $who] == [string tolower $nick]} {
     putserv "PRIVMSG $channel :Eh... Don't waste my time, slap yourself!"
     return 1
    }
    if {[string tolower $who] == [string tolower $botnick]} {
      putserv "PRIVMSG $channel :Eh, how funny..."
      return 1
    }
    if {[matchattr $who +n]} {
      putact $chan "loves his owner..."
      return 1
    }
    set randnum [expr [rand [expr (1000-500+1)]] + 500]
    set slaps {
        "drives over $who"
        "shreds $who with a chainsaw"
        "steals $who's cookies. mwahaha!"
        "pushes $who into a crevasse"
        "follows $who into a dark avenue. Hey test my crowbar!"
        "persuades $who to eat the cyanide sandwich"
        "slaps $who around a bit with a large trout"
        "forgets $who in the desert. Awww.."
        "is searching for $who + lame on Google. WEEE $randnum hits!"
        "burns $who's house down"
        "throws a keyboard at $who"
        "beautifies $who with some knuckle duster imprints"
        "sends hordes of lemmings over $who"
        "slaps $who around with a pink Macintosh"
        "slaps $who around with nuclear waste"
        "throws a playstation at $who"
        "throws a green xbox at $who"
        "sneezes in $who's face"
        "stabs $who in the face"
        "slaps $who around a bit with a small Imperial-Class Stardestroyer"
        "slaps $who around with Internet Explorer"
    }
    set slapoutput [lindex $slaps [rand [llength $slaps]]]
    if {$who == ""} {
      putserv "NOTICE $nick :Usage: slap <nick>"
      return 1
    }
    if {![onchan $who $channel]} {
      putserv "NOTICE $nick :I can't see $who on $channel! Time for new glasses maybe?"
      putserv "PRIVMSG $channel :eh.."
      return 1
    }
    putserv "PRIVMSG $channel :\001ACTION [subst $slapoutput]\001"
  }
}

#Slap sbody on #channel
proc polite:kiss {nick host handle channel testes} {
  if {([isop $nick $channel]) || ([ishalfop $nick $channel]) || ([isvoice $nick $channel])} {
    global botnick slaps slapoutput randnum
    set who [lindex $testes 2]
    if {[string tolower $who] == [string tolower $nick]} {
     putserv "PRIVMSG $channel :Eh... Don't waste my time, kiss yourself!"
     return 1
    }
    if {[string tolower $who] == [string tolower $botnick]} {
      putserv "PRIVMSG $channel :Eh, how funny..."
      return 1
    }
    if {[matchattr $who +n]} {
      putact $channel "is quite certain his owner loves him dearly..."
      return 1
    }
    set randnum [expr [rand [expr (1000-500+1)]] + 500]
    set slaps {
        "kisses $who passionately"
        "gives $who a long oily kiss"
        "spends some private time with $who"
	"grabs $who for a long and intimate kiss"
	"holds $who close for a long kiss"
	"kisses $who"
    }
    set slapoutput [lindex $slaps [rand [llength $slaps]]]
    if {$who == ""} {
      putserv "NOTICE $nick :Usage: slap <nick>"
      return 1
    }
    if {![onchan $who $channel]} {
      putserv "NOTICE $nick :I can't see $who on $channel! Time for new glasses maybe?"
      putserv "PRIVMSG $channel :eh.."
      return 1
    }
    putserv "PRIVMSG $channel :\001ACTION [subst $slapoutput]\001"
  }
}

proc polite:dalek {nick uhost hand chan text} {
  set kickedmsgs {
   {We are the superior beings!}
   {Exterminate! Annihilate! Destroy!}
   {Exterminate! Exterminate! Exterminate!}
   {There is only one form of life that matters - Dalek life.}
   {Advance and attack! Attack and destroy! Destroy and rejoice!}
   {The only interest we have in you is your total extermination!}
   {EXTERMINATE! DALEKS CONQUER AND DESTORY! DALEKS CONQUER AND DESTROY!}
   {Take them! Take them! We are the masters of earth. We are the masters of earth!}
   {Pity? I have no understanding of the word. It is not registered in my vocabulary bank. Exterminate!}
   {Our plan has worked. We shall triumph! You will obey, YOU WILL OBEY!}
   {You will take the Dalek Factor. You will spread it to the entire history of Earth!}
   {Primative weapons moderately effective!}
   {Our programming does not permit us to acknowledge that any creature is superior to the Daleks!}
   {Activate the Omega device!}
   {Genetic material extrapolated initiate cellular reconstruction!!}
   {We have your associate. You will obey, or she will be exterminated!}
   {EXT-ER-MI-NATE! EXT-ER-MI-NATE! EXT-ER-MI-NATE! EXT-ER-MI-NATE!}
   {Do not blaspheme! Do not blaspheme! Do not blaspheme!}
   {Location: Earth! Lifeforms detected! Exterminate! Exterminate! Exterminaaaaaate!}
   {Exterminate all lifeforms below! EXTERMINAAATE!}
   {Firepower restored! Found primary target! EXTERMINATE!}
  }

  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    if {[botisop $chan]} {
      if {[string length $text] > 0} {
        set tnick [lindex $text 2]
  
        if {$tnick == "me"} {
          set tnick $nick
        }

	if {$tnick == "flow"} {
          set reason "Do not blaspheme! Do not blaspheme! Do not blaspheme!"
          putkick $chan $nick $reason
	}

        if {[isbotnick $tnick]} {
  	  return 0
        }
        if {([isop $tnick $chan]) || ([ishalfop $tnick $chan]) || ([isvoice $tnick $chan])} {
          return 0
        }
        set reason [lindex $kickedmsgs [rand [llength $kickedmsgs]]]
        if {[onchan $tnick $chan]} { 
          putkick $chan $tnick $reason
        } else { 
          puthelp "NOTICE $nick :$tnick is no longer on $chan"
        }
      } else { 
        puthelp "NOTICE $nick :usage explained"
      }
    } else { 
      puthelp "NOTICE $nick :i dont have ops!" 
    }
  }
}

proc polite:thanks {nick uhost hand chan text} {
  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    set thanksmsgs {
     {no problem,}
     {you're welcome,}
     {anytime,}
     {always for you,}
     {I tip my hat to you,}
    }

    if {[onchan $nick $chan]} { 
      set reason [lindex $thanksmsgs [rand [llength $thanksmsgs]]]
      putserv "PRIVMSG $chan :$reason $nick"
    }
  }
}

proc polite:kick {nick uhost hand chan text} {
  if {[botisop $chan]} {

    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      if {$tnick == "me"} {
        set tnick $nick
      }
      if {[isbotnick $tnick]} {
	return 0
      }

      # Take a reason for kicking
      if {[string length [lindex $text 3]] == 0} { 
        set reason "Kick requested by $nick"
      } else { 
        set reason [lrange $text 3 end] 
      }

      if {[onchan $tnick $chan]} { 
        putkick $chan $tnick $reason
      } else { 
        puthelp "NOTICE $nick :$tnick is no longer on $chan"
      }
    } else { 
      puthelp "NOTICE $nick :usage explained"
    }
  } else { 
    puthelp "NOTICE $nick :i dont have ops!" 
  }
}

proc polite:op {nick uhost hand chan text} {
  if {[botisop $chan] == 1} {
    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      if {$tnick == "me"} {
        set tnick $nick
      }
      if {[isbotnick $tnick]} {
        return 0
      }

      if {[onchan $tnick $chan]} {
        pushmode $chan +o $tnick
      } else { 
        puthelp "NOTICE $nick :$tnick is not on $chan" 
      }
    } else { 
      puthelp "NOTICE $nick :Please tell me who you want to give ops" 
    }
  } else { 
    puthelp "NOTICE $nick :i dont have ops!" 
  }
}

proc polite:deop {nick uhost hand chan text} {
  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    if {[botisop $chan] == 1} {
      if {[string length $text] > 0} {
        set tnick [lindex $text 2]
        if {$tnick == "me"} {
          set tnick $nick
        }
        if {[isbotnick $tnick]} {
          return 0
        }

        if {[onchan $tnick $chan]} {
          if {[isop $tnick $chan]} {
            pushmode $chan -o $tnick
          }
        } else { 
          puthelp "NOTICE $nick :$tnick is not on $chan" 
        }
      } else { 
        puthelp "NOTICE $nick :Please tell me who you want to deop" 
      }
    } else { 
      puthelp "NOTICE $nick :i dont have ops!" 
    }
  }
}

proc polite:voice {nick uhost hand chan text} {
  if {[botisop $chan] == 1} {
    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      if {$tnick == "me"} {
        set tnick $nick
      }
      if {[isbotnick $tnick]} {
        return 0
      }

      if {[onchan $tnick $chan]} {
        if {![isvoice $tnick $chan]} {
          pushmode $chan +v $tnick
        }
      } else { 
        puthelp "NOTICE $nick :$tnick is not on $chan" 
      }
    } else { 
      puthelp "NOTICE $nick :Please tell me who you want to give voice" 
    }
  } else { 
    puthelp "NOTICE $nick :i dont have ops!" 
  }
}

proc polite:devoice {nick uhost hand chan text} {
  if {[botisop $chan] == 1} {
    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      if {$tnick == "me"} {
        set tnick $nick
      }
      if {[isbotnick $tnick]} {
        return 0
      }

      if {[onchan $tnick $chan]} {
        if {[isvoice $tnick $chan]} {
          pushmode $chan -v $tnick
        }
      } else { 
        puthelp "NOTICE $nick :$tnick is not on $chan" 
      }
    } else { 
      puthelp "NOTICE $nick :Please tell me from who you want to take voice" 
    }
  } else { 
    puthelp "NOTICE $nick :i dont have ops!" 
  }
}

proc polite:ban {nick uhost hand chan text} {
  if {[botisop $chan]} {
    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      #set thost [getchanhost $tnick $chan]
      set thost [maskhost [getchanhost $tnick $chan]]

      ## People we do not want to ban
      if {[isbotnick $tnick]} {
        return 0
      } 
      if {[matchattr $tnick +n] || [matchattr $tnick +m] || [matchattr $tnick +a] || [matchattr $tnick +o] || [matchattr $tnick +g] || [matchattr $tnick +v]} {
	return 0
      }

      ## Banning should be safe
      putserv "MODE $chan +b $thost"

      ## Get rid of the user
      set reason "Ban requested by $nick. Have a nice day."
      if {[onchan $tnick $chan]} { 
        putkick $chan $tnick $reason
      } else { 
        puthelp "NOTICE $nick :$tnick is no longer on $chan"
      }
    } else { 
      puthelp "NOTICE $nick :usage explained"
    }
  } else { 
    puthelp "NOTICE $nick :i dont have ops!" 
  }
}

proc polite:xban {nick uhost hand chan text} {
  if {[botisop $chan]} {
    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      #set thost [getchanhost $tnick $chan]
      set thost [maskhost [getchanhost $tnick $chan]]

      ## People we do not want to ban
      if {[isbotnick $tnick]} {
        return 0
      } 
      if {[matchattr $tnick +n] || [matchattr $tnick +m] || [matchattr $tnick +a] || [matchattr $tnick +o] || [matchattr $tnick +g] || [matchattr $tnick +v]} {
	return 0
      }

      ## Banning should be safe
      if {[string length [lindex $text 3]] == 0} { 
        set reason "Ban requested by $nick. Have a nice day."
      } else { 
        set reason [lrange $text 3 end] 
      }
      putserv "PRIVMSG X :ban $chan $tnick 24 75 $reason (24h ban)"
    } else { 
      puthelp "NOTICE $nick :usage explained"
    }
  } else { 
    puthelp "NOTICE $nick :i dont have ops!" 
  }
}

proc polite:unban {nick uhost hand chan text} {
  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    if {[botisop $chan]} {
      if {[string length $text] > 0} {
        set tnick [lindex $text 2]
        putserv "NOTICE $nick :Attempting to unban $tnick in $chan"
	killchanban $chan $tnick
        #putserv "MODE $chan -b $tnick"
      } else { 
        puthelp "NOTICE $nick :What person or userhost do you want to unban?"
      }
    } else {
      puthelp "NOTICE $nick :I dont have ops!" 
    }
  } 
}

proc polite:xunban {nick uhost hand chan text} {
  if {[botisop $chan]} {
    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      putserv "PRIVMSG X :unban $chan $tnick"
    } else { 
      puthelp "NOTICE $nick :What person or userhost do you want to unban?"
    }
  } else { 
    puthelp "NOTICE $nick :i dont have ops!" 
  }
}

proc polite:music {nick uhost hand chan text} {
  set dancemsgs {
   {is grooving along as hard as it can!}
   {dances along to the great tunes}
   {bounces up and down through the channel}
   {loves what it is hearing!}
   {can not get enough of this!}
   {hopes the music will not stop for a while}
  }

  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    set reason [lindex $dancemsgs [rand [llength $dancemsgs]]]
    putact $chan "$reason"
  }
}

proc polite:invite {nick uhost hand chan text} {
  if {[botisop $chan]} {
    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      putserv "invite $tnick $chan"
      putserv "PRIVMSG $chan :Inviting $tnick to $chan"
    } else { 
      puthelp "NOTICE $nick :What person do you want to invite to $chan?"
    }
  } else { 
    puthelp "NOTICE $nick :i dont have ops!" 
  }
}

proc polite:time {nick host handle chan arg} {
  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    set who [time]
    putserv "PRIVMSG $chan :The current time is $who CET"
    return 1
  }
}

proc polite:server {nick host handle channel testes} {
  if {([isop $nick $channel]) || ([ishalfop $nick $channel]) || ([isvoice $nick $channel])} {
    global server
    putserv "PRIVMSG $channel :I'm currently on $server."
    return 1
  }
}

proc polite:date {nick host handle channel testes} {
  if {([isop $nick $channel]) || ([ishalfop $nick $channel]) || ([isvoice $nick $channel])} {
    set who [date]
    putserv "PRIVMSG $channel :Today is: $who."
    return 1
  }
}

# restart the bot
proc polite:restart {nick host handle channel testes} {
  if {[matchattr $nick +n]} {
    putserv "PRIVMSG $channel :Restarting the bot, $nick"
    restart
  }
}

# reload the userfile
proc polite:reload {nick host handle channel testes} {
  if {[matchattr $nick +n]} {
    reload
    putserv "PRIVMSG $channel :Reload done, $nick"
  }
}

# rehash the bot
proc polite:rehash {nick host handle channel testes} {
  if {[matchattr $nick +n]} {
    rehash
    putserv "PRIVMSG $channel :Rehash done, $nick"
  }
}

# save the configfiles
proc polite:save {nick host handle channel testes} {
  if {[matchattr $nick +n]} {
    save
    putserv "PRIVMSG $channel :Save done, $nick"
  }
}

# backup the configfiles
proc polite:backup {nick host handle channel testes} {
  if {[matchattr $nick +n]} {
    backup
    putserv "PRIVMSG $channel :Backup done, $nick"
  }
}

# die
proc polite:die {nick host handle channel testes} {
  if {[matchattr $nick +n]} {
    set reason [lindex $testes 2]
    putserv "PRIVMSG $channel :Shutdown requested by $nick"
    die $reason
  }
}

# Replies to the bot
bind ctcr - PING ping_me_reply

proc polite:ping {nick uhost hand chan text} {
  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    global pingchan pingwho
    if {[string length $text] > 0} {
      set tnick [lindex $text 2]
      if {$tnick == "me"} {
        putserv "PRIVMSG $nick :\001PING [unixtime]\001"
        set pingwho 0
      } else {
        putserv "PRIVMSG $text :\001PING [unixtime]\001"
        set pingwho 1
      }
    } else {
      puthelp "NOTICE $nick :Which person do you want to ping?"
    }
    set pingchan $chan
  }
}

proc ping_me_reply {nick host handle dest key arg} {
  global pingchan pingwho
  if {![regexp "\[^0-9\]" $arg]} {
    if {!$pingwho} {
      puthelp "PRIVMSG $pingchan :$nick\: You're [expr [unixtime] - $arg] seconds lagged."
    } else {puthelp "PRIVMSG $pingchan :$nick is [expr [unixtime] - $arg] seconds lagged."}
  }
  return 1
}

proc polite:uptime {nick host handle chan arg} {
  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    global uptime botnick
    putserv "PRIVMSG $chan :Uptime for $botnick: [duration [expr [unixtime]-$uptime]]"
  }
}

proc polite:whoami {nick host handle channel arg} {
  if {([isop $nick $channel]) || ([ishalfop $nick $channel]) || ([isvoice $nick $channel])} {
    global botnick
    putserv "NOTICE $nick :You are $nick !$host"
    if {[validuser $handle]} {
      putserv "NOTICE $nick :Your handle on $botnick is $handle with [chattr $handle $channel] flags."
      putserv "NOTICE $nick :Your hosts are [getuser $handle HOSTS]"
      if {[matchattr $handle n] || [matchattr $handle m]} {putserv "NOTICE $nick :You are my owner or a master"}
      set info [getuser $handle INFO]
      if {$info == ""} {return 0}
      putserv "NOTICE $nick :Your global INFO line is $info"
    }
  }
}

proc polite:resetbans {nick host handle channel arg} {
  putserv "NOTICE $nick :Now resetting all bans on $channel to match my banlist..."
  resetbans $channel
}

# Changes the channel settings for a channel
# Usage: chanset <chan> <what> [args]
proc polite:chanset {nick host hand chan arg} {
  if {![matchattr $nick +n]} {
    return 0
  }
  # Make sure we get enough arguments
  if {[llength $arg] < 3} {
    notice $nick "Usage: chanset <mode> \[args\]"
    return 0
  }

  set mode [lindex $arg 2]

  if {![validchan $chan]} {
    notice $nick "I don't monitor that channel."
    return 0
  }
  putserv "PRIVMSG $chan :Setting $mode on $chan"
  #channel set $chan $mode
}

proc polite:adduser {nick host hand chan arg} {
  set tnick [lindex $arg 2]
  set host [lindex $arg 3]
  set ip [getchanhost $tnick]
  if {$host == ""} {
    set kadd [adduser $tnick]; 
    adduser $tnick
  } elseif {$host == "defaults"} {
    set kadd [adduser $tnick *!$ip]; 
    adduser *!$tnick $ip
  } else {
    set kadd [adduser $tnick $host]; 
    adduser $tnick $host
  }
  if {$host != "defaults"} {
    if {$host == ""} {
      if {$kadd == 1} {
        putserv "privmsg $chan :$nick: Successfully added user $tnick with no host."
      } else {
        putserv "privmsg $chan :$nick: Error, could not add user $tnick (already exists)"
      }
    } else {
      if {$kadd == 1} {
        putserv "privmsg $chan :$nick: Successfully added user $tnick with host $host"
      } else {
        putserv "privmsg $chan :$nick: Error, could not add user $nick (already exists)"
      }
    }
  }
  if {$host == "defaults"} {
    if {$kadd == 1} {
      putserv "privmsg $chan :$nick Successfully added user $tnick with host *!$ip."
    } else {
      putserv "privmsg $chan :$nick: Error, could not add user $tnick, he already existz!!"
    }
  }
}

proc polite:deluser {nick host hand chan arg} {
  if {![matchattr $nick +n]} {
    return 0
  }
  set tnick [lindex $arg 2]
  set dels [deluser $tnick]; 
  deluser $tnick
  if {$dels == 1} {
    putserv "privmsg $chan :$nick: User $tnick has been successfully deleted."
  } else {
    putserv "privmsg $chan :$nick: User $tnick is not in my database."
  }
}

proc polite:chattr {nick host hand chan arg} {
  if {![matchattr $nick +n]} {
    return 0
  }
  set tnick [lindex $arg 2]
  set change [lindex $arg 3]
  set tchan [lindex $arg 4]
  if {$tchan == ""} {
    set chattz [chattr $tnick $change]; 
    chattr $tnick $change
    putserv "privmsg $chan :$nick: The global flags for $tnick are now: $chattz"
  } else {
    set chattz [chattr $tnick |$change $tchan]; 
    chattr $tnick |$change $tchan
    putserv "privmsg $chan :$nick: The flags for $tnick in $tchan are now: $chattz"
  }
}

proc polite:addchan {nick host hand chan arg} {
  if {![matchattr $nick +n]} {
    return 0
  }
  set tchan [lindex $arg 2]
  set tadmin [lindex $arg 3]
  if {![validchan $tchan]} {
    channel add $tchan
    savechannels
    if {![validuser $tadmin]} {
      chattr $tnick |no $tchan
      putserv "privmsg $chan :$nick: Added channel $tchan and updated flags for admin $tadmin"
    } else {
      set ip [getchanhost $tadmin]
      adduser *!$tadmin $ip
      chattr $tadmin |no $tchan
      putserv "privmsg $chan :$nick: Added channel $tchan with admin $tadmin"
    }
  }
}

proc polite:delchan {nick host hand chan arg} {
  if {![matchattr $nick +n]} {
    return 0
  }
  set tchan [lindex $arg 2]
  if {[validchan $tchan]} {
    channel remove $tchan
    savechannels
    putserv "privmsg $chan :$nick: Deleted channel $tchan from my database."
  }
}

proc polite:validuser {nick host hand chan arg} {
  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    set tnick [lindex $arg 2]
    set valible [validuser $tnick]
    if {$valible == 1} {
      putserv "privmsg $chan :$nick: $tnick is a valid user."
    } else {
      putserv "privmsg $chan :$nick: $tnick is not a valid user."
    }
  }
}


# Says something on any channel
proc polite:say {nick host hand chan arg} {
  global botnick
  if {[llength $arg] < 3} {
    return 0
  }
  set thechan [lindex $arg 2]
  set what [lrange $arg 3 end]
  if {![onchan $botnick $thechan]} {
    notice $nick "I'm not on that channel."
    return 0
  }
  puthelp "PRIVMSG $chan :$what"
}

proc polite:pm {nick uhost hand chan text} {
  if {([isop $nick $chan]) || ([ishalfop $nick $chan]) || ([isvoice $nick $chan])} {
    if {[string length $text] > 0} {
      set tnick [lindex $text 0]
      if {[isbotnick $tnick]} {
        return 0
      }

      if {[onchan $tnick $chan]} {
        putserv "privmsg $chan :$tnick: Do *not* private message people without a good reason!"
      } else { 
        puthelp "NOTICE $nick :$tnick is not on $chan" 
      }
    } else { 
      puthelp "NOTICE $nick :Please tell me who you want warn for PM abuse."
    }
  }
}

putlog "* obot_chat.tcl v$script_version by flow"
